package com.allen.study.domain.service;

import com.allen.study.common.base.DomainResponse;
import com.allen.study.domain.entity.Department;
import com.allen.study.domain.repository.IDepartmentRepo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * 部门信息表领域服务
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class DepartmentService {

    /**
     * 部门信息表资源库
     */
    private final IDepartmentRepo departmentRepo;

    /**
     * 创建部门信息表
     *
     * @param department 部门信息表实体
     * @return 响应结果
     */
    public DomainResponse<Void> create(@NotNull Department department) {
        // 获取当前登录用户
        // SecurityUser securityUser = SecurityUserHelper.get();
        // 设置租户
        // department.setTenantId(securityUser.getTenantId());

        // 保存部门信息表
        departmentRepo.create(department);
        return DomainResponse.ok();
    }

    /**
     * 根据主键删除部门信息表
     *
     * @param departmentId 部门信息表主键
     * @return 响应结果
     */
    public DomainResponse<Void> deleteById(@NotNull String departmentId) {
        // 根据主键查询部门信息表
        Department department = departmentRepo.queryById(departmentId);
        // 部门信息表数据存在且租户正确，删除部门信息表数据
        if (ObjectUtils.isNotEmpty(department) && department.checkTenant()) {
            departmentRepo.deleteById(departmentId);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键更新部门信息表
     *
     * @param department 部门信息表实体
     * @return 响应结果
     */
    public DomainResponse<Void> updateById(@NotNull Department department) {
        // 根据主键查询部门信息表
        Department exists = departmentRepo.queryById(department.getId());
        // 部门信息表数据存在且租户正确，更新部门信息表数据
        if (ObjectUtils.isNotEmpty(exists) && exists.checkTenant()) {
            departmentRepo.updateById(department);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键查询部门信息表
     *
     * @param departmentId 部门信息表主键
     * @return 响应结果
     */
    public DomainResponse<Department> queryById(@NotNull String departmentId) {
        Department department = departmentRepo.queryById(departmentId);
        return DomainResponse.ok(department);
    }
}
